package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.entity.DmDmContractDay;
import cn.com.cifi.mars.entity.DmOdsNosTradedataviewformars;
import cn.com.cifi.mars.entity.NosViewTradedataviewformars;
import cn.com.cifi.mars.entity.VDmDmContractDay;
import cn.com.cifi.mars.mapper.mars.DmDmContractDayMapper;
import cn.com.cifi.mars.service.DmDmContractDayService;
import cn.com.cifi.mars.service.VDmDmContractDayService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author plx
 * @since 2019-12-17
 */
@Service
public class DmDmContractDayServiceImpl extends ServiceImpl<DmDmContractDayMapper, DmDmContractDay> implements DmDmContractDayService {

    private static final Logger logger = LoggerFactory.getLogger(DmDmContractDayServiceImpl.class);

    @Autowired
    private VDmDmContractDayService vDmDmContractDayService;



    @Override
    public void sync() {
        //清空
        remove(new QueryWrapper<>());
        logger.info("清空DmDmContractDay数据完毕");

        //同步
        logger.info("同步DmDmContractDay数据开始");
        int pageNo = 0;
        int pageSize = 3000;
        Page<VDmDmContractDay> page = new Page<>(pageNo, pageSize);
        do {
            page.setCurrent(++pageNo);
            vDmDmContractDayService.page(page);
            logger.info("总数：{}，总批次：{}，当前批次：{}，每批数量：{},本批数量：{}", page.getTotal(), page.getPages(), page.getCurrent(), page.getSize(), page.getRecords().size());
            if (!page.getRecords().isEmpty()) {
                List<DmDmContractDay> list = page.getRecords().parallelStream()
                        .map(x -> {
                            DmDmContractDay dest = new DmDmContractDay();
                            try {
                                PropertyUtils.copyProperties(dest, x);
                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                            }
                            return dest;
                        }).collect(Collectors.toList());
                saveBatch(list);
            }
        } while (page.hasNext());
        logger.info("同步DmDmContractDay数据结束");
    }

    @Override
    public void syncV2() {
        //清空
        remove(new QueryWrapper<>());
        logger.info("清空DmDmContractDay数据完毕");

        //同步
        logger.info("同步DmDmContractDay数据开始");
        baseMapper.insertSync();
        logger.info("同步DmDmContractDay数据结束");
    }

    @Override
    public void syncV3(long batchSize) {
        //清空
        remove(new QueryWrapper<>());
        logger.info("清空DmDmContractDay数据完毕");

        //同步
        long pageNo = 0;
        long pageSize = batchSize;
        Page<VDmDmContractDay> page = new Page<>(pageNo, pageSize);
        logger.info("同步DmDmContractDay数据开始");
        do {
            page.setCurrent(++pageNo);
            vDmDmContractDayService.page(page);
            logger.info("总数：{}，总批次：{}，当前批次：{}，每批数量：{},本批数量：{}", page.getTotal(), page.getPages(), page.getCurrent(), page.getSize(), page.getRecords().size());
            if (!page.getRecords().isEmpty()) {
                List<DmDmContractDay> list = page.getRecords().parallelStream()
                        .map(x -> {
                            DmDmContractDay dest = new DmDmContractDay();
                            try {
                                PropertyUtils.copyProperties(dest, x);
                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                            }
                            return dest;
                        }).collect(Collectors.toList());
                saveBatch(list);
            }
        } while (page.hasNext());
        logger.info("同步DmDmContractDay数据结束");
    }
}
